<?php

class Pages extends MX_Controller {

    function __construct() {
        parent::__construct();
    }

    /**
     * Chuẩn bị layout, các phần dữ liệu cần thiết cho module này
     *
     * @param type $method
     * @param type $para1
     * @param type $para2
     */
    function dispatcher($method = '', $para1 = NULL, $para2 = NULL, $para3 = NULL) {
        $main_content = NULL;
        $layout = 'layout/content_layout';
        $lang = $this->_lang;
        $breadcrumbs = '';
        $data = NULL;
        switch ($method) {
            case 'page_detail' :
                $uri = '/' . (trim($para2) != '' ? $para1 . '/' . $para2 : $para1 ) . '.htm';
                $main_content = $this->get_page_detail($data, array('uri' => trim($uri)));
                $breadcrumbs = modules::run('breadcrumbs/breadcrumbs_with_param', array(
                            array(
                                'uri' => $data['uri'],
                                'name' => $data['title']
                            ),
                            array(
                                'uri' => get_base_url(),
                                'name' => __('IP_home_page')
                            )
                        ));
                break;
            case 'book_room':
                $main_content = $this->message();
                
        }

        $view_data = $this->get_genaral_content(array('lang' => $lang, 'current_menu' => '/' . $this->uri->uri_string()));
        $view_data['breadcrumbs'] = $breadcrumbs;
        $view_data['main_content'] = $main_content;
        $view_data['title'] = $this->_title;
        $view_data['keywords'] = $this->_keywords;
        $view_data['description'] = $this->_description;

        if (MOBILE_SITE) {
            $layout = 'mobile/content_layout';
        } else {
            
        }

        $this->load->view($layout, $view_data);
    }

    public function get_genaral_content($options = array()) {
        $view_data = array();
        $view_data['main_menu'] = modules::run('menus/menus/get_main_menus', $options);
        $view_data['left_side'] = $this->_get_left_content($options);
        if (MOBILE_SITE) {
            $view_data['bottom_menus'] = modules::run('menus/menus/get_bottom_menus', $options + array('show_title' => TRUE));
        }
        return $view_data;
    }

    private function _get_left_content($options = array()) {
        $output = '';
        $output = $this->book_form($options);
        return $output;
    }

    function get_page_data($options = array()) {
        $output = array();
        $pages = $this->pages_model->get_pages($options);
        foreach ($pages as $page) {
            $output[$page['uri']] = $page;
        }
        return $output;
    }

    function get_page_detail(& $_data, $options = array()) {
        $uri = $options['uri'];
        $pages = get_cache('pages');
        if (!isset($pages[$uri]))
            return show_404();
        $page = $pages[$uri];

        $_data = $page;

        $view_data = array();
        $view_data['page'] = $page;

        $this->_title = $page['meta_title'] != '' ? $page['meta_title'] : $page['title'] . DEFAULT_TITLE_SUFFIX;
        $this->_keywords = $page['meta_keywords'] != '' ? $page['meta_keywords'] : $page['title'];
        $this->_description = $page['meta_description'] != '' ? $page['meta_description'] : strip_tags($page['summary']);

        $layout = MOBILE_SITE ? 'pages/mobile/page_detail' : 'pages/page_detail';

        return $this->load->view($layout, $view_data, TRUE);
    }

    function get_static_page_content($options = array()) 
    {
        $output = '';
        $uri = $options['uri'];
        $pages = get_cache('pages');
        if (isset($pages[$uri])) 
        {
            if (isset($options['get_summary']))
                $output = $pages[$uri]['summary'];
            else
                $output = $pages[$uri]['content'];
        }
        return $output;
    }
    
    function book_form($options = array())
    {
        if($this->is_postback())
        {
            $this->_book_room();
        }
        $view_data = array();
        $view_data['day_combo'] = $this->utility_model->get_days_combo(array('day' => date('d')));
        $view_data['month_combo'] = $this->utility_model->get_months_combo(array('month' => date('m')));
        $view_data['year_combo'] = $this->utility_model->get_years_combo(array('year' => date('Y'), 'from_year' => '2013', 'to_year' => date('Y')));
        return $this->load->view('booking_form', $view_data, TRUE);
    }
    
    private function _book_room()
    {
        $this->form_validation->set_rules('day', 'Ngày', 'is_not_default_combo');
        $this->form_validation->set_rules('month', 'Tháng', 'is_not_default_combo');
        $this->form_validation->set_rules('year', 'Năm', 'is_not_default_combo');
        $this->form_validation->set_rules('name', 'Họ và tên', 'trim|required');
        $this->form_validation->set_rules('phone', 'Điện thoại', 'trim|required|numeric');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
        if ($this->form_validation->run())
        {
            $this->_send_email();
            redirect('/dat-phong?msg=success');
        }
        redirect('/dat-phong?msg=fail');
    }
    
    function message()
    {
        return $this->load->view('message', NULL, TRUE);
    }
    
    private function _send_email()
    {
        $options = array();
        $this->load->library('email');
        $options['name'] = $this->input->post('name');
        $options['phone'] = $this->input->post('phone');
        $options['day'] = $this->input->post('day');
        $options['month'] = $this->input->post('month');
        $options['year'] = $this->input->post('year');
        $options['num_day'] = $this->input->post('num_day');
        $options['persons'] = $this->input->post('persons');
        $options['num_room'] = $this->input->post('num_room');
        $title      = 'Yêu cầu đặt phòng nhà nghỉ '  . DOMAIN_NAME;
        $message    = $this->load->view('book_room_email', $options, TRUE);

        $config        = modules::run('configurations/get_configuration', array('array' => TRUE));
        $contact_email = ($config['contact_email'] != '' && $config['contact_email'] != NULL) ? $config['contact_email'] : CONTACT_EMAIL;

        $this->email->from($this->input->post('email'), $options['name']);
        $this->email->subject($title);
        $this->email->message($message);
        $this->email->to($contact_email);
        $this->email->send();
    }

}

?>
